﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ENG.TeachersDiary
{
  public partial class MarkComboBox : UserControl
  {
    private class MyItem
    {
      public Model.MarkCode Code;
      public MyItem(Model.MarkCode code)
      {
        this.Code = code;
      }
      public override string ToString()
      {
        return Code.Display;
      }
    }

    public MarkComboBox()
    {
      InitializeComponent();

      FillComboBox();
    }

    private bool innerResize = false;
    private void MarkComboBox_Resize(object sender, EventArgs e)
    {
      if (innerResize) return;
      innerResize = true;

      this.Height = this.ComboBox.Height;

      innerResize = false;
    }

    private void FillComboBox()
    {
      if (DesignMode)
      {
        ComboBox.Items.Clear();

        ComboBox.Items.Add("1");
        ComboBox.Items.Add("2");
        ComboBox.Items.Add("3");
        ComboBox.Items.Add("4");
        ComboBox.Items.Add("5");
      }
      else
      {
        Model.MarkCode last = this.SelectedValue;

        ComboBox.Items.Clear();

        var db = Common.DBConn;
        var mcbs = db.MarkCodes.OrderBy(i => i.OrderIndex);

        foreach (var fItem in mcbs)
        {
          if (this.ShowNotValuableMarks == false)
            if (fItem.IsEvaluable == false)
              continue;

          ComboBox.Items.Add(new MyItem(fItem));

          this.SelectedValue = last;
        } // foreach (var fItem in mcbs)
      }
    }

    [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Advanced)]
    private bool _ShowNotValuableMarks;
    ///<summary>
    /// Sets/gets ShowNotValuableMarks value.
    ///</summary>
    public bool ShowNotValuableMarks
    {
      get
      {
        return (_ShowNotValuableMarks);
      }
      set
      {
        _ShowNotValuableMarks = value;
        FillComboBox();
      }
    }

    public Model.MarkCode SelectedValue
    {
      get
      {
        if (ComboBox.SelectedIndex < 0)
          return null;
        else
        {
          Model.MarkCode ret = (ComboBox.SelectedItem as MyItem).Code;

          return ret;
        }
      }
      set
      {
        if (value == null)
          ComboBox.SelectedIndex = -1;
        else
          ComboBox.SelectedItem = value;
      }
    }
  }
}
